<?php
    /*-------------------------------------------------------+
     | PHP-Fusion Content Management System
     | Copyright (C) 2002 - 2011 Nick Jones
     | http://www.php-fusion.co.uk/
     +--------------------------------------------------------+
     | Filename: admin_reset.php
     | Author: Nick Jones (Digitanium)
     +--------------------------------------------------------+
     | This program is released as free software under the
     | Affero GPL license. You can redistribute it and/or
     | modify it under the terms of this license which you
     | can read by viewing the included agpl.txt or online
     | at www.gnu.org/licenses/agpl.html. Removal of this
     | copyright header is strictly prohibited without
     | written permission from the original author(s).
     +--------------------------------------------------------*/
    require_once "../maincore.php";
    
    if (!checkrights("SM") || !defined("iAUTH") || !isset($_GET['aid']) || $_GET['aid'] != iAUTH) { redirect("../index.php"); }
    
    require_once THEMES."templates/admin_header.php";
    
    if (isset($_GET['status']) && !isset($message)) {
        if ($_GET['status'] == "pw") {
            $message = $locale['411'];
        }
        if ($message) {	echo "<div id='close-message'><div class='admin-message'>".$message."</div></div>\n"; }
    }
    
    if (isset($_POST['validation_code']) && isset($_POST['days']))
    {
        
        $validation_code = (isset($_POST['validation_code']) ? stripinput($_POST['validation_code']) : "");
        $days = (isset($_POST['days']) ? stripinput($_POST['days']) : "0");
        
        opentable("Validation Code Addition");
        
        if ($validation_code != "" && $days != "0")
        {
            echo "<table cellpadding='0' cellspacing='0' width='70%' class='admin-reset tbl-border center'>\n";
            echo "<tr>\n";
            echo "<td class='tbl1' width='250'><strong>Added Code:</strong></td>\n";
            echo "<td class='tbl1'>".$validation_code."</td>\n";
            echo "</tr>\n";
            echo "</table>\n";
			
            $result = dbquery("INSERT INTO ".DB_PREFIX."validation_codes (validation_code, validation_datestamp) VALUES ('".$validation_code."', ".(time()+(86400*intval($days))).")");
        }
        else
        {
            echo "<table cellpadding='0' cellspacing='0' width='70%' class='admin-reset tbl-border center'>\n";
            echo "<tr>\n";
            echo "<td class='tbl1' width='250'><strong>Unable To Add Code:</strong></td>\n";
            echo "<td class='tbl1'>".$validation_code."</td>\n";
            echo "</tr>\n";
            echo "</table>\n";
        }
        
        closetable();
    }
    else if (isset($_POST['delete']) && isset($_POST['ident']))
    {
        
        $validation_id = (isset($_POST['ident']) ? stripinput($_POST['ident']) : "");
        
        opentable("Validation Code Addition");
        
        if ($validation_id != "")
        {
            $result = dbarray(dbquery("SELECT validation_code FROM ".DB_PREFIX."validation_codes WHERE validation_id = ".$validation_id." LIMIT 1"));
            
            echo "<table cellpadding='0' cellspacing='0' width='70%' class='admin-reset tbl-border center'>\n";
            echo "<tr>\n";
            echo "<td class='tbl1' width='250'><string>Deleted Code (".$validation_id."):</strong></td>\n";
            echo "<td class='tbl1'>".$result['validation_code']."</td>\n";
            echo "</tr>\n";
            echo "</table>\n";
            
            $result = dbquery("DELETE FROM ".DB_PREFIX."validation_codes WHERE validation_id = ".$validation_id." LIMIT 1");
        }
        else
        {
            $result = dbarray(dbquery("SELECT validation_code FROM ".DB_PREFIX."validation_codes WHERE validation_id = ".$validation_id." LIMIT 1"));
            echo "<table cellpadding='0' cellspacing='0' width='70%' class='admin-reset tbl-border center'>\n";
            echo "<tr>\n";
            echo "<td class='tbl1' width='250'><strong>Unable To Delete Code (".$validation_id."):</strong></td>\n";
            echo "<td class='tbl1'>".$result['validation_code']."</td>\n";
            echo "</tr>\n";
            echo "</table>\n";
        }
        
        closetable();
    }
	
	echo "<script language='javascript' type='text/javascript'>
	function generateCode()
	{
	var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz';
	var string_length = 40;
	var randomstring = '';
	for (var i=0; i<string_length; i++)
	{
	var rnum = Math.floor(Math.random() * chars.length);
	randomstring += chars.substring(rnum,rnum+1);
	}
	
	$(\"#validation_code\").val(randomstring);
	}
	</script>\n";
    
    opentable("Add Validation Code");
    
    echo "<form name='validation_code' method='post' action='".GenerateSecureLink(FUSION_SELF.$aidlink)."''>\n";
    echo "<table cellpadding='0' cellspacing='0' width='70%' class='admin-reset tbl-border center'>\n<tr>\n";
    echo "<td class='tbl1' width='250' valign='top'><label for='validation_code'>Validation Code:</label></td>\n";
    echo "<td class='tbl1' width='250'><input type='text' size='100' value='' name='validation_code' id='validation_code'></td>\n";
    echo "</tr>\n<tr>";
    echo "<td class='tbl1' width='250' valign='top'><label for='days'>How many days untill the code expires:</label></td>\n";
    echo "<td class='tbl1' width='250'><select id='days' name='days' class='textbox'>";
    
    echo "<option value='1'>1 Day</option>\n";
    echo "<option value='2'>2 Days</option>\n";
    echo "<option value='3'>3 Days</option>\n";
    echo "<option value='4'>4 Days</option>\n";
    echo "<option value='5'>5 Days</option>\n";
    echo "<option value='6'>6 Days</option>\n";
    echo "<option value='7'>7 Days</option>\n";
    echo "<option value='8'>8 Days</option>\n";
    echo "<option value='9'>9 Days</option>\n";
    echo "<option value='10'>10 Days</option>\n";
    
    echo "</select></td>\n";
    
    echo "</tr>\n<tr>\n";
    echo "<td class='tbl1' width='250' valign='top'></td>\n";
    echo "<td class='tbl1'><input type='submit' name='validation' id='submit' value='Add Code' class='button' /><input type='button' name='validation_generator' id='validation_generator' value='Generate Code' class='button' onclick='generateCode()' /></td>\n";
    echo "</tr>\n</table>\n</form>\n";
    
    closetable();
    
    opentable("Validation Codes");
    
    echo "<table cellpadding='0' cellspacing='0' width='70%' class='admin-reset tbl-border center'>\n<tr>\n";
    echo "<td class='tbl2' valign='top'><strong>Validation ID</strong></td>\n";
    echo "<td class='tbl2' valign='top'><strong>Validation Code</strong></td>\n";
    echo "<td class='tbl2' valign='top'><strong>Validation Expiration Date</strong></td>\n";
    echo "<td class='tbl2' valign='top'><strong>Delete Validation Code</strong></td>\n";
    echo "</tr>\n";
    
    $result = dbquery("SELECT validation_id, validation_code, validation_datestamp FROM ".DB_PREFIX."validation_codes ORDER BY validation_id DESC");
    $i = 1;
    while ($data = dbarray($result)) {
        $row_color = ($i % 2 == 0 ? "tbl2" : "tbl1");
        
        echo "<form name='validation_code' method='post' action='".GenerateSecureLink(FUSION_SELF.$aidlink)."''>\n";
        echo "<tr>\n";
        echo "<td class='".$row_color."' valign='top'>".$data['validation_id']."</td>\n";
        echo "<td class='".$row_color."' valign='top'>".$data['validation_code']."</td>\n";
        echo "<td class='".$row_color."' valign='top'>".showdate("shortdate", $data['validation_datestamp'])."</td>\n";
        echo "<td class='".$row_color."' valing='top'>";
        echo "<input type='submit' name='delete' id='delete' value='Delete' class='button' />\n";
        echo "</td></tr>\n";
        
        echo "<input type='hidden' name='ident' id='ident' value='".$data['validation_id']."' />\n";
        echo "</form>";
        
        $i++;
    }
    
    echo "</table>\n";
    
    closetable();
    
    require_once THEMES."templates/footer.php";
    ?>
